package com.lanswon.qzsmk.controller.cardManageController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanswon.qzsmk.base.CardContants;
import com.lanswon.qzsmk.base.DwzPage;
import com.lanswon.qzsmk.base.HttpContants;
import com.lanswon.qzsmk.base.SystemManageContants;
import com.lanswon.qzsmk.exception.ApplicationException;
import com.lanswon.qzsmk.model.ActiveUser;
import com.lanswon.qzsmk.model.AddressStreet;
import com.lanswon.qzsmk.model.AppKind;
import com.lanswon.qzsmk.model.CardPhysicsType;
import com.lanswon.qzsmk.model.CardProdunctDefinition;
import com.lanswon.qzsmk.model.Certificate;
import com.lanswon.qzsmk.model.CooperationCompany;
import com.lanswon.qzsmk.model.Org;
import com.lanswon.qzsmk.service.cardManageService.MakeCardService;
import com.lanswon.qzsmk.service.cardManageService.SellCardService;
import com.lanswon.qzsmk.service.systemManageService.BranchManageService;
import com.lanswon.qzsmk.service.systemManageService.ParamManageService;
import com.lanswon.qzsmk.vo.MakeCardQueryConditionVo;
import com.lanswon.qzsmk.vo.OrgCorpVo;
import com.lanswon.qzsmk.vo.PersonalVo;
import com.lanswon.qzsmk.vo.ReadCardVo;
import com.lanswon.qzsmk.vo.ResultObject;
import com.lanswon.qzsmk.vo.ResultVo;
import com.lanswon.qzsmk.vo.ServerTypeVo;

@Controller
public class BaseQueryController  {
	
	@Autowired
	MakeCardService mcs;
	
	@Autowired
	ParamManageService pm;
	
	@Autowired
	BranchManageService bms;
	
	@Autowired
	SellCardService scs;
	/**
	 * 动态获取卡物理类型、卡产品集合、证件类型集合(整个系统都有共用)
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(name="基础查询动态获取卡",value="/applicationSingle/getCardInfo",method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object>getCardInfo(HttpServletRequest request){
		
		String param = (String)request.getSession().getAttribute("param");
		System.out.println("param====>"+param);
		request.getSession().removeAttribute("param");
		Map<String,String> rmap = new HashMap<String,String>();
		if(param!=null&&!"".equals(param)){
			//解析参数
			
			String[] paramArray = param.split("&");
			for(int i=0;i<paramArray.length;i++){
				String[] items = paramArray[i].split("=");
				rmap.put(items[0], items[1]);
			}
			
		}
		List<CardPhysicsType> cdPhysicsTypes=null;
		ResultVo resultVo=pm.queryAllCardPhysicsType();//卡物理类型集合
		if(resultVo.getResultStatus().getCode()==HttpContants.RETURN_SUCCESS){
			cdPhysicsTypes=(List<CardPhysicsType>)((ResultObject)resultVo.getServiceObject()).getItems();
		}		
		List<CardProdunctDefinition> cdProDefinitions=null;
		resultVo=pm.queryAllCardProdunctDefinition();//卡产品集合
		if(resultVo.getResultStatus().getCode()==HttpContants.RETURN_SUCCESS){
			cdProDefinitions=(List<CardProdunctDefinition>)((ResultObject)resultVo.getServiceObject()).getItems();
		}
		List<Certificate> certificates=null;
		resultVo=pm.queryAllCertificates();//证件类型集合
		if(resultVo.getResultStatus().getCode()==HttpContants.RETURN_SUCCESS){
			certificates=(List<Certificate>) ((ResultObject)resultVo.getServiceObject()).getItems();
		}
		
		Map<String, Object> map=new HashMap<>();
		map.put("cdPhysicsTypes", cdPhysicsTypes);
		map.put("cdProDefinitions", cdProDefinitions);
		map.put("certificates", certificates);
		map.put("paperType", rmap.get("paperType"));
		map.put("paperNo", rmap.get("paperNo"));
		map.put("cardTypeCode", rmap.get("cardTypeCode"));
		map.put("cardCode", rmap.get("cardCode"));
		
		return map;
	}
	
	/**
	 * 获取网点集合(增加网点和增加柜员有共用，后续会有增加)
	 * @return
	 * @throws ApplicationException 
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(name="基础查询网点信息",value="/tellerManage/getpaperAndBranch",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getPaperAndBranch() throws ApplicationException{
		Map<String, Object> map=new HashMap<>();
		List<Certificate> certificates=null;
		List<ServerTypeVo> serverTypes =null;
		List<OrgCorpVo> orgCorps =null;
		List<AddressStreet> AddressStreet=pm.queryAddressStreetByFatherId(SystemManageContants.cityCode);
		ResultVo resultVo=pm.queryAllCertificates();//证件类型集合
		if(resultVo.getResultStatus().getCode()==HttpContants.RETURN_SUCCESS){
			certificates=(List<Certificate>) ((ResultObject)resultVo.getServiceObject()).getItems();
		}
		List<Org> branchs=null;
		resultVo=bms.queryBranch(null,null);
		if(resultVo.getResultStatus().getCode()==HttpContants.RETURN_SUCCESS){
			branchs=(List<Org>)((ResultObject)resultVo.getServiceObject()).getItems();
		}
		//服务类型
		resultVo=bms.queryOrgServerType();
		if(resultVo.getResultStatus().getCode()==HttpContants.RETURN_SUCCESS){
			serverTypes=(List<ServerTypeVo>)((ResultObject)resultVo.getServiceObject()).getItems();
		}
		//网点所属单位
		resultVo=bms.queryOrgCorp();
		if(resultVo.getResultStatus().getCode()==HttpContants.RETURN_SUCCESS){
			orgCorps=(List<OrgCorpVo>)((ResultObject)resultVo.getServiceObject()).getItems();
		}
		map.put("certificates", certificates);
		map.put("branchs", branchs);
		map.put("AddressStreet", AddressStreet);
		map.put("serverTypes", serverTypes);
		map.put("orgCorps", orgCorps);
		return map;
	}
	
	/**
	 * 动态获取卡产品集合集合(在制卡任务导出，空白卡制卡共用)
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(name="基础查询动态获取卡产品信息",value="/makeCard/getCardInfo",method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object>getCardInfoByMakeCard(HttpServletRequest request){
		List<CardProdunctDefinition> cdProDefinitions=null;
		ResultVo resultVo=pm.queryAllCardProdunctDefinition();//卡产品集合
		if(resultVo.getResultStatus().getCode()==HttpContants.RETURN_SUCCESS){
			cdProDefinitions=(List<CardProdunctDefinition>)((ResultObject)resultVo.getServiceObject()).getItems();
		}
		Map<String, Object> map=new HashMap<>();
		map.put("cdProDefinitions", cdProDefinitions);
		return map;
	}
	/**
	 * 读卡通过卡号查询信息(售卡页面，单笔发卡，后续有其他页面)
	 * @param citizenCardNo
	 * @return
	 * @throws ApplicationException 
	 */
	@RequestMapping(value="/sellCard/queryCardData",name="基础查询读卡通过卡号查询信息",method=RequestMethod.POST)
	@ResponseBody
	public  Map<String, Object> queryCardDataByCitizenCardNo(String citizenCardNo) throws ApplicationException{
		Map<String, Object> map=new HashMap<>();
		map.put("statusCode", "200");
		ReadCardVo cardData=scs.queryCardDataByCitizenCardNo(citizenCardNo);
		map.put("cardData", cardData);
		return map;
	}
	
	/**
	 * 获取个人信息(dialog),(批量制卡任务生成，和批量制卡任务导出共用)
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/makeCard/getInfoByApplyNO",name="基础查询获取个人信息(dialog)",method=RequestMethod.POST)
	@ResponseBody
	public DwzPage<PersonalVo> getInfoByApplyNO(HttpServletRequest request){
		@SuppressWarnings("unchecked")
		DwzPage<PersonalVo> dPage= (DwzPage<PersonalVo>) request.getSession().getAttribute("dPage");
		request.getSession().removeAttribute("dPage");
		return dPage;
	}
	
	/**
	 * dialog分页刷新(申请批次号/任务编号),(批量制卡任务生成，和批量制卡任务导出共用)
	 * @param numPerPage
	 * @return
	 */
	@RequestMapping(value="/makeCard/pagingByApplyNo",name="基础查询dialog分页刷新(申请批次号/任务编号)",method=RequestMethod.POST)
	@ResponseBody
	public DwzPage<PersonalVo> pagingByApplyNo(MakeCardQueryConditionVo mcv,Integer pageNum,Integer numPerPage){
		System.out.println(mcv);
//		if(mcv.getCurrentPage()<=1||pageCount==0){
//			mcv.setCurrentPage(1);
//		}else if(mcv.getCurrentPage()>=pageCount&&pageCount!=0){
//			mcv.setCurrentPage(pageCount);
//		}
		mcv.setCurrentPage(pageNum);
		mcv.setNumPerPage(numPerPage);
		mcv.setStartLine((mcv.getCurrentPage()-1)*mcv.getNumPerPage());
		DwzPage<PersonalVo> dPage=null;
		if(mcv.getApplyNo()!=null){
			dPage=mcs.queryPersonalByApplybatchNo(mcv);
		}else{
			dPage=mcs.queryPersonalByCardTaskNo(mcv);
		}
		
		return dPage;
	}
	
	/**
	 * 动态获取卡产品,制卡商，银行信息集合(批量制卡任务生成和增加空白卡任务共用)
	 * @param request
	 * @return
	 * @throws Exception 
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/makeCardBatch/getCardCodeANDCompany",name="基础查询获取卡产品制卡商银行信息集合",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object>getCardInfo() throws Exception{
		List<CooperationCompany> companies=mcs.queryCooperationCompanyByType("1");
		List<CooperationCompany> banks=mcs.queryCooperationCompanyByType("2");
		List<CardPhysicsType> cdPhysicsTypes=null;
		ResultVo resultVo=pm.queryAllCardPhysicsType();//卡物理类型集合
		if(resultVo.getResultStatus().getCode()==HttpContants.RETURN_SUCCESS){
			cdPhysicsTypes=(List<CardPhysicsType>)((ResultObject)resultVo.getServiceObject()).getItems();
		}		
		List<CardProdunctDefinition> cdProDefinitions=null;
		resultVo=pm.queryAllCardProdunctDefinition();//卡产品集合
		if(resultVo.getResultStatus().getCode()==HttpContants.RETURN_SUCCESS){
			cdProDefinitions=(List<CardProdunctDefinition>)((ResultObject)resultVo.getServiceObject()).getItems();
		}
		Map<String, Object> map=new HashMap<>();
		map.put("cdProDefinition", cdProDefinitions);
		map.put("companies", companies);
		map.put("banks", banks);
		map.put("cdPhysicsTypes", cdPhysicsTypes);
		return map;
	}

	/**
	 * 动态展示卡位置和卡状态(单笔申领查询和卡状态信息查询共用)
	 * @return
	 */
	@RequestMapping(value="/queryInfo/getCardStautsAndPosition",name="动态展示卡位置和卡状态",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getCardStautsAndPosition(){
		Map<String, Object> cardpositions=CardContants.getCardPositions();
		Map<String, Object> cardStautses=CardContants.getCardStatuses();
		Map<String, Object> map=new HashMap<>();
		map.put("cardpositions", cardpositions);
		map.put("cardStautses", cardStautses);
		return map;
	}
	
	/**
	 * 链接单笔发卡登记页面(与换卡当场发卡共用)
	 * @return
	 */
	
	@RequestMapping(value="/issueCardSingle/getCardRegister",name="单笔发卡登记页面",method=RequestMethod.POST)
	public String getCardRegister(){
		
		return "GetCardRegister";
	}
	
	/**
	 * 获取操作员信息
	 * @return
	 */
	@RequestMapping(value="/getOperator",method=RequestMethod.POST)
	@ResponseBody
	public ActiveUser getOperator(){
		ActiveUser activeUser  = (ActiveUser) SecurityUtils.getSubject().getSession().getAttribute("activeUser");
		return activeUser;
	}
	
	/**
	 * 获取所有应用
	 * @return
	 */
	@RequestMapping(value="/getAppKind",method=RequestMethod.POST)
	@ResponseBody
	public List<AppKind> getAppKind(){
		List<AppKind> appkinds=pm.queryAppKind();
		return appkinds;
	}
	
	/**
	 * 获取批次号
	 */
	@RequestMapping(value="/queryBatchNo",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> queryBatchNo(String cardPCode,String cardPhysicalType){
		ResultVo vo=pm.generateApplyBatchNo(cardPCode, cardPhysicalType);
		Map<String, Object>map=new HashMap<>();
		if(vo.getResultStatus().getCode()==(HttpContants.RETURN_SUCCESS)){
			map.put("statuscode", HttpContants.RETURN_SUCCESS);
			map.put("serviceObject", vo.getServiceObject());
		}
		return map;	
	}
	
	/**
	 * 获取要修改的信息
	 * @param 
	 * @return
	 */
	@RequestMapping(value="makeCardBlank/getUpdateInfo",name="空白卡制卡获取要修改的信息",method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getUpdateInfo(HttpServletRequest request){
		Map<String, Object> map=new HashMap<>();
		map.put("sign", request.getSession().getAttribute("sign"));
		map.put("mcpv", request.getSession().getAttribute("mcpv"));
		request.getSession().removeAttribute("sign");
		request.getSession().removeAttribute("mcpv");
		return map;
	}
}
